package org.example.campsys.controller;

import org.example.campsys.model.Result;
import org.example.campsys.model.UserInfo;
import org.example.campsys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class UserController {
     @Autowired
     public UserService userService;


    @RequestMapping("/login")//登录
    public Result login(String loginName, String password , HttpSession session){
        if (!StringUtils.hasLength(loginName)|| !StringUtils.hasLength(password)){
            return Result.fail("输入账号或密码不能为空！",-1);
        }
        UserInfo userInfo = userService.queryUserByName(loginName);
        userInfo.setRoleId(userService.getRoleId(userInfo.getId()));
        //验证账号密码
        if (userInfo==null){
            return Result.fail("发生错误！",-1);
        }
        if (password.equals(userInfo.getPassword())){
            userInfo.setPassword("");
            session.setAttribute("userName",userInfo);
            return Result.success("登录成功",userInfo);
        }
        return Result.fail("账号或密码错误！",-1);
    }

}
